/**********************************************************************
 * userid 工号
 * ts 时间戳
 * sign 签名
 * @url: /services/apexrest/rest
 * @data:
 *  {
        {
        "data":{"userid":"User","ts":"1","sign":""}
   }
    }
    {"data":{"userid":"9999","ts":"1550645177289","sign":"89f75c2f937451d667d10dd55421371a"}}

*************************************************************************/
@RestResource(urlMapping='/CRM/querySFMeasageREST/*')
global with sharing class QuerySFMeasageREST {
    @HttpPost
    global static RestReturnMessage querySFMeasage(String userid ,String ts ,String sign) {

    	System.debug(userid);
    	///1参数验证
    	if(userid == null || ts == null || sign  == null )  return  setResult('参数错误!');

    	String sign1= EncodingUtil.convertToHex(Crypto.generateDigest('MD5',Blob.valueOf(ts+userid + ts)));

    	if(sign1!= sign )return setResult('参数错误!') ;

        if((Long.valueOf(ts) - System.now().getTime() ) /1000 > 600 )return setResult('参数异常!') ;

    	///2.用户检验
    	String uid = EncodingUtil.base64Decode(userid).toString();
    	User u = new User();
    	try {

    	     u = [Select Id from User  where ExuserCode__c =:uid  ];

    	}catch (Exception e) {

    		return setResult('没有SF账号或账号维护异常!') ;
    	}

    	Map<String,List<Map<String,String>>> resList = new Map<String,List<Map<String,String>>>();
    	///3.查找待审批信息
    	List<Map<String,String>> pwList = new List<Map<String,String>>();

    	List<ProcessInstanceWorkitem> pws  = [select id ,processinstance.createddate,
    	processinstance.processdefinition.Name,processinstance.submittedby.Name ,
        actorid , processinstance.targetobjectId,processinstance.targetobject.Name
    	 from ProcessInstanceWorkitem where actorid=:u.id  Order by  processinstance.createddate desc ];
System.debug('==>'+ pws.size() );
    	for (ProcessInstanceWorkitem pw  :pws ) {
    		Map<String,String> pm = new Map<String,String>();
    		pm.put('whatName',pw.processinstance.targetobject.Name );
    		pm.put('whatId', pw.processinstance.targetobjectId );
    		pm.put('whatType', pw.processinstance.processdefinition.Name);
    		pm.put('approval','/p/process/ProcessInstanceWorkitemWizardStageManager?id='+pw.Id);
    		pm.put('submitName', pw.processinstance.submittedby.Name);
    		pm.put('submitDate', pw.processinstance.createddate == null ? '': pw.processinstance.createddate.format('yyyy年MM月dd日'));
    		pwList.add(pm);
    	}
    	resList.put('Approvals',pwList);

        Date d1 = Date.newInstance(2019, 1, 1);
    	///4.查找任务通知
    	List<Map<String,String>>  tList = new List<Map<String,String>>();
		for(Task t:[select Id,Status,What.Name ,Subject,WhatId  from Task  where OwnerId =:u.id  and Status='Open'  and ActivityDate >=:d1    Order by ActivityDate desc  ] ){
			Map<String,String> tm = new Map<String,String>();

    		tm.put('subject',t.Subject );
			tm.put('status',t.Status );
    		tm.put('whatName',t.What.Name  );
    		tm.put('whatId',t.WhatId );
    		tm.put('taskId',t.Id  );

			tList.add(tm);
		}
		resList.put('Tasks',tList);

        return  setResult(resList); 
    }

    /*global class ProcessInstanceWorkitemBean {

    	global String whatName ; //相关项
    	global String whatId ; //相关项Id 
    	global String whatType ; //类型
    	global String submitName ; //提交人 
    	global String submitDate ; //提交日期 
    	global ProcessInstanceWorkitemBean (ProcessInstanceWorkitem pw ){
    		whatName = pw.processinstance.targetobject.Name ; 
    		whatId = pw.processinstance.targetobjectId  ; 
    		whatType = pw.processinstance.processdefinition.Name; 
    		submitName = pw.processinstance.submittedby.Name ; 
    		submitDate =  pw.processinstance.createddate == null ? '': pw.processinstance.createddate.format('yyyy年MM月dd日');
    	}

    }
    global class TaskBean {
    	global String subject ; //主题
    	global String status ; //状态
    	global String  whatName  ; //相关项 ; 
    	global String  whatId ; //相关项Id; 
    	global String  taskId ; //任务Id; 
    	global TaskBean (Task t ){
    		subject = t.Subject ; 
    		status = t.Status ; 
    		taskId = t.Id ; 
    		whatId = t.WhatId ; 
    		whatName = t.What.Name ; 
    	}

    }*/
   global static RestReturnMessage setResult(String msg){
   		RestReturnMessage res = new RestReturnMessage();
   		res.result = '0';
   		res.result_msg = msg; 

   		
   		return res ; 

   }
    global static RestReturnMessage setResult(Map<String,List<Map<String,String >>> o ){
   		RestReturnMessage res = new RestReturnMessage();
   		res.result = '1';
   		res.result_msg = 'success'; 
   		res.dataMap = o ; 
   		
   		return res ; 

   }
 



}